<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Model\SystemConfig;

class Settle extends BaseModel
{
    protected $connection = 'api';
    protected $unionTable = 'union';
    protected $unionSettleTable = 'union_settle';

    //公会结算审核
    public function unionSettleAudit($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['id'])) {
                $msg = '参数错误';
                break;
            }
            $res = curl_request(getKkaudioApiUrl() . '/inner/settle/unionSettleAudit', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //获取公会结算列表
    public function getUnionSettleList(Request $request)
    {
        $date_st = $request->input('date_st');
        $date_ed = $request->input('date_ed');
        $union_id = $request->input('union_id');
        $user_id = $request->input('user_id');
        $status = $request->input('status');
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);

        $query = DB::connection($this->connection)->table($this->unionSettleTable);
        if(!empty($date_st)) {
            $query = $query->where('settle_date_st', $date_st);
        }
        if(!empty($date_ed)) {
            $query = $query->where('settle_date_ed', $date_ed);
        }
        if(!empty($union_id)) {
            $query = $query->where('union_id', $union_id);
        }
        if(!empty($user_id)) {
            $query = $query->where('user_id', $user_id);
        }
        if(isset($status) && is_numeric($status)) {
            $query = $query->where('status', $status);
        }
        $result = $query->orderBy('id', 'desc')->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        $kk_total = $query->sum('settle_kk');
        if(!empty($result['data'])) {
            $unionIds = array_column($result['data'], 'union_id');
            $unionList = DB::connection($this->connection)->table($this->unionTable)->whereIn('id', $unionIds)->get();
            $unionList = array_column(objToArray($unionList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $unionInfo = empty($unionList[$val['union_id']]) ? [] : $unionList[$val['union_id']];

                $val['union_name'] = empty($unionInfo['name']) ? '' : $unionInfo['name'];
                $val['settle_docx'] = build_image($val['settle_docx']);
                $val['confirm_img'] = build_image($val['confirm_img']);
                $val['invoice_img'] = build_image($val['invoice_img']);
                
                $result['data'][$key] = $val;
                
            }
        }
        $result['kk_total'] = number_format($kk_total/10,2);
        return $result;
    }

}
